package com.zw.admin.server.dao;

import com.zw.admin.server.commom.TkMapper;
import com.zw.admin.server.dto.chart.GuardChart;
import com.zw.admin.server.model.Device;
import com.zw.admin.server.model.Guard;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Mapper
@Repository
public interface GuardDao extends TkMapper<Guard> {

    @Select("select orgName from bsn_guard_event group by orgName")
    List<String> getAllOrgName();

    @Select("select * from bsn_guard_event where orgName = '${orgName}'")
    List<Guard> getGuardInfoByOrg(@Param("orgName") String orgName);

    @Select("select * from bsn_guard_event where (devName = \"签到打卡\" or devName = \"签退打卡\") and eventTime between '${startTime}'  and '${endTime}'" +
            " limit ${start}, ${end}")
    List<Guard> getGuardInfoByTime(@Param("startTime") String startTime,
                                   @Param("endTime") String endTime,
                                   @Param("start") Integer start,
                                   @Param("end") Integer end);

    @Select("select count(*) from bsn_guard_event where (devName = \"签到打卡\" or devName = \"签退打卡\") and eventTime between '${startTime}'  and '${endTime}'")
    Integer getGuardInfoCount(@Param("startTime") String startTime, @Param("endTime") String endTime);

    @Select("select orgName, DATE_FORMAT(eventTime, '%Y-%m-%d') date, count(*) checkNum\n" +
            "from bsn_guard_event\n" +
            "where devName = \"签到打卡\"\n" +
            "and eventTime between '${startTime}'  and '${endTime}'"+
            "group by orgName, DATE_FORMAT(eventTime, '%Y%m%d')")
    List<GuardChart> getChartData(@Param("startTime") String startTime,
                                  @Param("endTime") String endTime);

    @Select({
            "<script>",
            "select count(*)",
            "from bsn_guard_event",
            "where orgName in",
            "<foreach collection='orgNames' item='name' open='(' separator=',' close=')'>",
            "#{name}",
            "</foreach>",
            "and (devName = \"签到打卡\" or devName = \"签退打卡\") ",
            "and eventTime between '${startTime}'  and '${endTime}'",
            "</script>"
    })
    Integer getGuardCountByTimeAndOrg(@Param("startTime") String startTime,
                                      @Param("endTime") String endTime,
                                      @Param("orgNames") List<String> orgNames);

    @Select({
            "<script>",
            "select *",
            "from bsn_guard_event",
            "where orgName in",
            "<foreach collection='orgNames' item='name' open='(' separator=',' close=')'>",
            "#{name}",
            "</foreach>",
            "and (devName = \"签到打卡\" or devName = \"签退打卡\") ",
            "and eventTime between '${startTime}'  and '${endTime}'",
            " limit ${start}, ${end}",
            "</script>"
    })
    List<Guard> getGuardByTimeAndOrg(@Param("startTime") String startTime,
                                     @Param("endTime") String endTime,
                                     @Param("start") Integer start,
                                     @Param("end") Integer end,
                                     @Param("orgNames") List<String> orgNames);
}
